<template>
<!-- 推就行了 -->
  <div class='container'>
    <div class="app-container">
    <el-card>
      <!-- 新增按钮 -->
    <el-row type="flex" justify="end" style="margin-bottom:20px">
      <el-col :span="1.5">
          <el-button size="small"  icon="el-icon-edit" type="success" @click="$router.push('new')">新增试题</el-button>
      </el-col>
    </el-row>
      <!-- 筛选功能模块 -->
    <el-form ref="refForm"  :model="form" label-width="80px" :inline="true" >

    <el-row  type="flex"  justify="space-between">
      <el-col :span="6">
        <el-form-item  label="学科" prop="subjectID">
        <el-select  placeholder="请选择" v-model="form.subjectID">
          <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="二级目录" prop="catalogID">
        <el-select  placeholder="请选择" v-model="form.catalogID">
          <el-option v-for="item in direcList" :key='item.id' :label="item.directoryName" :value="item.id"></el-option>
        </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="标签" prop="tags">
        <el-select  placeholder="请选择" v-model="form.tags">
          <el-option v-for="item in tagList" :key='item.id' :label="item.tagName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item placeholder="请选择" label="关键字" prop="keyword">
         <el-input style="width:221px" v-model="form.keyword"></el-input>
      </el-form-item>
      </el-col>
    </el-row>
    <!--  -->
     <el-row type="flex"  justify="space-between">
      <el-col :span="6">
        <el-form-item  label="试题类型" prop="questionType">
        <el-select  placeholder="请选择" v-model="form.questionType">
          <el-option  v-for="item in constants.questionType" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="难度"  prop="difficulty">
        <el-select  placeholder="请选择" v-model="form.difficulty">
          <el-option v-for="item in constants.difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="方向" prop="direction">
        <el-select  placeholder="请选择" v-model="form.direction">
          <el-option v-for="item in constants.direction" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item placeholder="请选择" label="录入人" prop="creatorID">
         <el-select  placeholder="请选择" v-model="form.creatorID">
          <el-option v-for="item in user" :key="item.id" :label="item.username" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      </el-col>
    </el-row>
    <!--  -->
     <el-row type="flex" justify="space-between">
      <el-col :span="6">
        <el-form-item  label="题目备注" prop="remarks">
        <el-input v-model="form.remarks"></el-input>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item  label="企业简称" prop="shortName">
        <el-input v-model="form.shortName"></el-input>
      </el-form-item>
      </el-col>

      <el-col :span="6">
        <div class="twoSelect">
      <el-form-item label="城市" prop="province">
        <el-select  placeholder="请选择" v-model="form.province">
          <el-option v-for="item in provinces" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="city">
        <el-select  placeholder="请选择" v-model="form.city">
          <el-option v-for="item in citysList" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
       </div>
      </el-col>

     <el-col :span="6" class="button">
      <el-form-item >
        <el-row :gutter="10">
          <el-col :span="12"><el-button @click="clearForm">清除</el-button></el-col>
          <el-col :span="12"><el-button type="primary" @click="searchSubject">搜索</el-button></el-col>
       </el-row>
      </el-form-item>
     </el-col>
    </el-row>

    </el-form>
    <!-- table标签页 -->
    <el-tabs  @tab-click="handleClick" v-model="activeName" type="card">
    <el-tab-pane name="all" label="全部">
    </el-tab-pane>
    <el-tab-pane name="checcg" label="待审核">
    </el-tab-pane>
      <el-tab-pane name="succeedCheccg" label="已审核">
      </el-tab-pane>
      <el-tab-pane name="reject" label="已拒绝">
      </el-tab-pane>
    </el-tabs>
    <!-- 表格 -->
      <el-table
      :data="otherList"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="number"
        label="试题编号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="subject"
        label="学科"
        width="120">
      </el-table-column>
      <el-table-column
        prop="catalog"
        label="目录"
        width="120">
      </el-table-column>
      <el-table-column
        prop="questionType"
        label="题型"
        width="120">
        <template #default="{row}">{{row.questionType | setQuestion}}</template>
      </el-table-column>
      <el-table-column prop="question" label="题干" width="300">
        <template #default='{row}'>
          <span v-html="row.question"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="录入时间"
        width="160">
        <template #default="{row}">{{row.addDate | parseTimeByString}}</template>
      </el-table-column>
      <el-table-column
        prop="difficulty"
        label="难度"
        width="120">
        <template #default="{row}">{{row.difficulty | setDifficulty}}</template>
      </el-table-column>
      <el-table-column
        prop="creator"
        label="录入人"
        width="120">
      </el-table-column>
      <el-table-column
        prop="chkState"
        label="审核状态"
        width="120">
        <template #default="{row}">{{row.chkState | setChkStates}}</template>
      </el-table-column>
      <el-table-column
        prop="chkRemarks"
        label="审核意见"
        width="120">
      </el-table-column>
      <el-table-column
        prop="chkUser"
        label="审核人"
        width="120">
      </el-table-column>
      <el-table-column
        prop="zip"
        label="发布状态"
        width="120">
         <template #default="{row}">
            {{row|issue}}
           </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template #default='{row}'>

          <el-button @click="showSubject(row)" type="text" >预览</el-button>
          <el-button type="text" size="small" :disabled="row.chkState === 1? true : false" @click="audit(row.id)">审核</el-button>
          <el-button type="text" size="small" :disabled="row.publishState === 1? true : false" @click="$router.push(`new?id=${row.id}`)">修改</el-button>
          <el-button @click="showGrounding(row)" type="text" size="small">{{row.publishState === 1? '下架' : '上架'}}</el-button>
          <el-button type="text" size="small" @click="delSubject(row)">删除</el-button>
        </template>
      </el-table-column>
      </el-table>
    <!--- 分页器  -->
    <el-row :gutter="20" type="flex" justify="end">
      <el-col :span="8.5" style="margin-top:15px">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="counts">
      </el-pagination>
      </el-col>
    </el-row>
    <!-- 预览 -->
    <questions-check  :rowData="rowData" :dialogVisible.sync ="dialogVisible"></questions-check>
    <!-- 审核对话框 -->
    <el-dialog
  title="题目预览"
  :visible="auditVisible"
  @close="showAudit"
  width="25%"
  >
  <el-form ref="ruleForm" :model="auditForm" :rules="rules"  label-width="20px">
  <el-form-item>
    <el-radio-group v-model="auditForm.chkState">
      <el-radio label="1">通过</el-radio>
      <el-radio label="2">拒绝</el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item prop="chkRemarks">
    <el-input type="textarea" v-model="auditForm.chkRemarks" ></el-input>
  </el-form-item>
</el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="showAudit">取 消</el-button>
    <el-button type="primary" @click="auditAffirm">确 定</el-button>
   </span>
</el-dialog>
    </el-card>
    </div>
  </div>
</template>

<script>
import { choice, remove, choicePublish, choiceCheck } from '@/api/hmmm/questions.js'
import { simple as list } from '@/api/hmmm/subjects.js'
import { directorysList } from '@/api/hmmm/directorys.js'
import { tagsList } from '@/api/hmmm/tags.js'
import * as constants from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { userList } from '@/api/base/users.js'
import questionsCheck from '../components/questions-check.vue'
export default {
  components: { questionsCheck },
  name: 'questions',
  filters: {
    // 题型处理
    setQuestion: (val) => {
      if (val === '1') return '单选'
      if (val === '2') return '多选'
      if (val === '3') return '简单'
    },
    // 难度处理
    setDifficulty: (val) => {
      if (val === '1') return '简单'
      if (val === '2') return '一般'
      if (val === '3') return '困难'
    },
    // 审核状态处理
    setChkStates (val) {
      if (val === 0) return '待审核'
      if (val === 1) return '已审核'
      if (val === 2) return '已拒绝'
    },
    // 发布状态处理
    issue (row) {
      if (row.publishState === 1 && row.chkState === 1) {
        return '已发布'
      }
      if (row.publishState === 0 && row.chkState === 1) {
        return '已下架'
      }
      return '待发布'
    }
  },
  data () {
    return {
      tableLiet: [], // 表格全部数据
      otherList: [], // 其他的数据
      form: {
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 企业所在地省份
        city: null // 企业所在城市
      },
      // 审核
      auditForm: {
        chkState: '1',
        chkRemarks: ''
      },
      choice: {},
      rules: {
        chkRemarks: [
          { required: true, message: '请输入审核意见', trigger: ['change', 'blur'] }
        ]
      },
      page: 1, // 页码
      pagesize: 5, // 单页数
      counts: 0, // 总数
      subjectList: [], // 学科列表
      direcList: [], // 目录列表
      tagList: [], // 标签列表
      constants: constants, // 常量文件,
      provinces: provinces(), // 所有的城市
      citysList: [], // 子级城市
      user: [], // 用户列表
      dialogVisible: false,
      rowData: {},
      newpublishState: null,
      auditVisible: false, // 审核对话框
      rowId: null, // 当前行的id
      activeName: 'all' // table标签页切换
    }
  },
  created () {
    this.getList()
    this.getSubjectList()
    this.getUserList()
  },
  methods: {
    // 获取学科列表数据
    async getSubjectList () {
      const { data } = await list()
      this.subjectList = data
    },
    // 获取表格列表数据
    async getList () {
      const { data } = await choice({ ...this.form, page: this.page, pagesize: this.pagesize })
      this.counts = data.counts
      this.otherList = this.tableLiet = data.items
    },
    // table 标签页切换
    handleClick () {
      const str = this.activeName
      if (str === 'all') {
        this.getList()
      } else if (str === 'checcg') {
        const arr = this.tableLiet.filter(item => item.chkState === 0)
        this.otherList = arr
      } else if (str === 'succeedCheccg') {
        const arr = this.tableLiet.filter(item => item.chkState === 1)
        this.otherList = arr
      } else if (str === 'reject') {
        const arr = this.tableLiet.filter(item => item.chkState === 2)
        this.otherList = arr
      }
    },
    // 获取用户列表
    async getUserList () {
      const { data } = await userList()
      this.user = data.list
    },
    // 单页数改变
    handleSizeChange (newval) {
      this.pagesize = newval
      // 重新渲染
      this.getList()
    },
    // 页码改变
    handleCurrentChange (newval) {
      this.page = newval
      this.getList()
    },
    // 清除
    clearForm () {
      console.log('111')
      this.form = {
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 企业所在地省份
        city: null // 企业所在城市
      }
    },
    // 搜索题目
    async searchSubject () {
      // const newObj = {}
      // for (const key in this.form) {
      //   if (this.form[key] !== null) {
      //     newObj[key] = this.form[key]
      //   }
      // }
      const { data } = await choice({ difficulty: '2' })
      console.log(data)
      this.otherList = data.items
      this.page = data.pafe
      this.pagesize = data.pagesize
      this.counts = data.pages
    },

    // 预览弹框
    showSubject (row) {
      this.dialogVisible = true
      this.rowData = row
    },
    // 删除功能
    async delSubject (row) {
      // 删除弹框
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消操作')
      // 发送删除请求
      const res = await remove(row)
      if (res.status === 200) {
        // 消息提示
        this.$message.success('删除成功')
        // 做判断
        // 刷新页面
        this.getList()
      }
    },
    // 上下架功能
    async showGrounding (row) {
      // this.newpublishState = row.publishState
      // console.log(row)
      if (row.publishState === 1) {
        // 弹框
        const str = await this.$confirm('您确认下架这道题目嘛?', '提示', {
          type: 'warning'
        }).catch(() => {})
        if (str !== 'confirm') return this.$message.info('已取消操作')
        // 下架请求
        row.publishState = 0
        const res = await choicePublish(row)
        if (res.status === 200) {
          // 消息提示
          this.$message.success('下架成功')
          // 刷新页面
          this.getList()
        }
      } else {
        // 弹框
        const str = await this.$confirm('您确认上架这道题目嘛??', '提示', {
          type: 'warning'
        }).catch(() => {})
        if (str !== 'confirm') return this.$message.info('已取消操作')
        row.publishState = 1
        // 上架请求
        const res = await choicePublish(row)
        if (res.status === 200) {
        // 消息提示
          this.$message.success('上架成功')
          // 刷新页面
          this.getList()
        }
      }
    },
    // 审核对话框关闭和重置
    showAudit () {
      this.auditVisible = false
      this.auditForm.chkState = '1'
      this.$refs.ruleForm.resetFields('chkRemarks')
    },
    // 点击审核
    audit (id) {
      // 打开对话框
      this.auditVisible = true
      this.rowId = id
    },
    // 确认审核
    auditAffirm () {
      // 预校验
      this.$refs.ruleForm.validate(async (boo) => {
        if (boo) {
          this.auditForm.chkState = +this.auditForm.chkState
          console.log({ ...this.auditForm, id: this.rowId })
          // 发送请求
          const res = await choiceCheck({ ...this.auditForm, id: this.rowId })
          console.log(res)
          // 提示
          if (!res.status === 200) this.$message.error('审核失败')
          this.$message.success('操作成功')
          // 关闭对话框并重置
          this.showAudit()
          // 渲染页面
          this.getList()
        }
      })
    }

  },
  watch: {
    // 监听from表单的值改变 渲染下拉框
    form: {
      immediate: true,
      deep: true,
      async handler (newval) {
        if (newval.subjectID) {
          // 值改变,发送获取目录列表和标签列表
          const { data } = await directorysList()
          // 获取标签列表
          const res = await tagsList()
          this.direcList = data.items
          this.tagList = res.data.items
        }
        if (newval.province) {
          this.citysList = citys(newval.province)
        }
      }
    }

  }

}
</script>

<style scoped lang='scss'>
.container {
  .app-container {
    .button {
      padding-left: 150px;
    }
    .twoSelect {
      ::v-deep .el-input--medium {
         width: 100px
       }

    }
  }
}
</style>
